home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / PostProcess / Scene.fx < prev   
Encoding:
Text File  |  2004-09-27  |  14.0 KB  |  536 lines

  1. //-----------------------------------------------------------------------------
  2. // File: Scene.fx
  3. //
  4. // Desc: Effect file for image post-processing sample.  This effect contains
  5. //       a technique that renders a scene with vertex and pixel shaders.
  6. //
  7. // Copyright (c) Microsoft Corporation. All rights reserved.
  8. //-----------------------------------------------------------------------------
  9.  
  10.  
  11.  
  12.  
  13. matrix  g_mRevView;
  14. matrix  g_mWorldView;
  15. matrix  g_mProj;
  16. texture g_txScene;
  17. texture g_txEnvMap;
  18. float3  g_vLightDirView = float3( 0.0f, 0.0f, -1.0f );
  19.  
  20.  
  21.  
  22.  
  23. sampler2D g_samScene =
  24. sampler_state
  25. {
  26.     Texture = <g_txScene>;
  27.     AddressU = Wrap;
  28.     AddressV = Wrap;
  29.     MinFilter = Point;
  30.     MagFilter = Linear;
  31.     MipFilter = Linear;
  32. };
  33.  
  34.  
  35. samplerCUBE g_samEnvMap =
  36. sampler_state
  37. {
  38.     Texture = <g_txEnvMap>;
  39.     AddressU = Wrap;
  40.     AddressV = Wrap;
  41.     MinFilter = Point;
  42.     MagFilter = Linear;
  43.     MipFilter = Linear;
  44. };
  45.  
  46.  
  47. samplerCUBE g_samSkyBox =
  48. sampler_state
  49. {
  50.     Texture = <g_txScene>;
  51.     AddressU = Wrap;
  52.     AddressV = Wrap;
  53.     MinFilter = Point;
  54.     MagFilter = Linear;
  55.     MipFilter = Linear;
  56. };
  57.  
  58.  
  59. void RenderSceneVS( float4 Pos : POSITION,
  60.                     float3 Normal : NORMAL,
  61.                     float2 Tex : TEXCOORD0,
  62.                     out float4 oPos : POSITION,
  63.                     out float4 Diffuse : COLOR,
  64.                     out float2 oTex : TEXCOORD0,
  65.                     out float3 oNormal : TEXCOORD1,
  66.                     out float3 oViewPos : TEXCOORD2 )
  67. {
  68.     // Output screen space position
  69.     oViewPos = mul( Pos, g_mWorldView );
  70.     oPos = mul( float4( oViewPos, 1.0f ), g_mProj );
  71.  
  72.     // Transform normal to view space
  73.     oNormal = mul( Normal, (float3x3)g_mWorldView );
  74.  
  75.     // Compute diffuse
  76.     Diffuse = dot( g_vLightDirView, oNormal );
  77.  
  78.     // Propagate tex coord
  79.     oTex = Tex;
  80. }
  81.  
  82.  
  83. void RenderScenePS( float4 Diffuse : COLOR,
  84.                     float2 Tex : TEXCOORD0,
  85.                     float3 Normal : TEXCOORD1,
  86.                     float3 Pos : TEXCOORD2,
  87.                     out float4 oCol : COLOR0,
  88.                     out float4 oNormal : COLOR1,
  89.                     out float4 oPos : COLOR2 )
  90. {
  91.     //
  92.     // Output pixel color
  93.     //
  94.     oCol = tex2D( g_samScene, Tex ) * Diffuse;
  95.     
  96.     //
  97.     // Output normal
  98.     //
  99.     oNormal = float4( normalize( Normal ), 1.0f );
  100.  
  101.     //
  102.     // Output view position
  103.     //
  104.     oPos = float4( Pos, 1.0f );
  105. }
  106.  
  107.  
  108. void RenderScenePS_ColorNormal( float4 Diffuse : COLOR,
  109.                                 float2 Tex : TEXCOORD0,
  110.                                 float3 Normal : TEXCOORD1,
  111.                                 float3 Pos : TEXCOORD2,
  112.                                 out float4 oCol : COLOR0,
  113.                                 out float4 oNormal : COLOR1 )
  114. {
  115.     //
  116.     // Output pixel color
  117.     //
  118.     oCol = tex2D( g_samScene, Tex ) * Diffuse;
  119.     
  120.     //
  121.     // Output normal
  122.     //
  123.     oNormal = float4( normalize( Normal ), 1.0f );
  124. }
  125.  
  126.  
  127. void RenderScenePS_Color( float4 Diffuse : COLOR,
  128.                           float2 Tex : TEXCOORD0,
  129.                           float3 Normal : TEXCOORD1,
  130.                           float3 Pos : TEXCOORD2,
  131.                           out float4 oCol : COLOR0 )
  132. {
  133.     //
  134.     // Output pixel color
  135.     //
  136.     oCol = tex2D( g_samScene, Tex ) * Diffuse;
  137. }
  138.  
  139.  
  140. void RenderScenePS_Normal( float4 Diffuse : COLOR,
  141.                            float2 Tex : TEXCOORD0,
  142.                            float3 Normal : TEXCOORD1,
  143.                            float3 Pos : TEXCOORD2,
  144.                            out float4 oNormal : COLOR0 )
  145. {
  146.     //
  147.     // Output normal
  148.     //
  149.     oNormal = float4( normalize( Normal ), 1.0f );
  150. }
  151.  
  152.  
  153. void RenderScenePS_Position( float4 Diffuse : COLOR,
  154.                              float2 Tex : TEXCOORD0,
  155.                              float3 Normal : TEXCOORD1,
  156.                              float3 Pos : TEXCOORD2,
  157.                              out float4 oPos : COLOR0 )
  158. {
  159.     //
  160.     // Output view position
  161.     //
  162.     oPos = float4( Pos, 1.0f );
  163. }
  164.  
  165.  
  166. void RenderEnvMapSceneVS( float4 Pos : POSITION,
  167.                           float3 Normal : NORMAL,
  168.                           float2 Tex : TEXCOORD0,
  169.                           out float4 oPos : POSITION,
  170.                           out float4 Diffuse : COLOR,
  171.                           out float3 oTex : TEXCOORD0,
  172.                           out float3 oNormal : TEXCOORD1,
  173.                           out float3 oViewPos : TEXCOORD2 )
  174. {
  175.     // Output screen space position
  176.     oViewPos = mul( Pos, g_mWorldView );
  177.     oPos = mul( float4( oViewPos, 1.0f ), g_mProj );
  178.  
  179.     // Transform normal to view space
  180.     oNormal = normalize( mul( Normal, (float3x3)g_mWorldView ) );
  181.  
  182.     // Compute diffuse
  183.     Diffuse = dot( g_vLightDirView, oNormal );
  184.  
  185.     // Compute reflective vector for tex coord
  186.     oTex = 2 * dot( -oViewPos, oNormal ) * oNormal + oViewPos;
  187.     oTex = mul( oTex, (float3x3)g_mRevView );
  188. }
  189.  
  190.  
  191. void RenderEnvMapScenePS( float4 Diffuse : COLOR,
  192.                           float3 Tex : TEXCOORD0,
  193.                           float3 Normal : TEXCOORD1,
  194.                           float3 Pos : TEXCOORD2,
  195.                           out float4 oCol : COLOR0,
  196.                           out float4 oNormal : COLOR1,
  197.                           out float4 oPos : COLOR2 )
  198. {
  199.     //
  200.     // Output pixel color
  201.     //
  202.     oCol = texCUBE( g_samEnvMap, Tex );// * Diffuse;
  203.  
  204.     //
  205.     // Output normal
  206.     //
  207.     oNormal = float4( normalize( Normal ), 1.0f );
  208.  
  209.     //
  210.     // Output view position
  211.     //
  212.     oPos = float4( Pos, 1.0f );
  213. }
  214.  
  215.  
  216. void RenderEnvMapScenePS_ColorNormal( float4 Diffuse : COLOR,
  217.                                       float3 Tex : TEXCOORD0,
  218.                                       float3 Normal : TEXCOORD1,
  219.                                       float3 Pos : TEXCOORD2,
  220.                                       out float4 oCol : COLOR0,
  221.                                       out float4 oNormal : COLOR1 )
  222. {
  223.     //
  224.     // Output pixel color
  225.     //
  226.     oCol = texCUBE( g_samEnvMap, Tex );// * Diffuse;
  227.  
  228.     //
  229.     // Output normal
  230.     //
  231.     oNormal = float4( normalize( Normal ), 1.0f );
  232. }
  233.  
  234.  
  235. void RenderEnvMapScenePS_Color( float4 Diffuse : COLOR,
  236.                                 float3 Tex : TEXCOORD0,
  237.                                 float3 Normal : TEXCOORD1,
  238.                                 float3 Pos : TEXCOORD2,
  239.                                 out float4 oCol : COLOR0 )
  240. {
  241.     //
  242.     // Output pixel color
  243.     //
  244.     oCol = texCUBE( g_samEnvMap, Tex );// * Diffuse;
  245. }
  246.  
  247.  
  248. void RenderEnvMapScenePS_Normal( float4 Diffuse : COLOR,
  249.                                  float3 Tex : TEXCOORD0,
  250.                                  float3 Normal : TEXCOORD1,
  251.                                  float3 Pos : TEXCOORD2,
  252.                                  out float4 oNormal : COLOR0 )
  253. {
  254.     //
  255.     // Output normal
  256.     //
  257.     oNormal = float4( normalize( Normal ), 1.0f );
  258. }
  259.  
  260.  
  261. void RenderEnvMapScenePS_Position( float4 Diffuse : COLOR,
  262.                                    float3 Tex : TEXCOORD0,
  263.                                    float3 Normal : TEXCOORD1,
  264.                                    float3 Pos : TEXCOORD2,
  265.                                    out float4 oPos : COLOR0 )
  266. {
  267.     //
  268.     // Output view position
  269.     //
  270.     oPos = float4( Pos, 1.0f );
  271. }
  272.  
  273.  
  274. //-----------------------------------------------------------------------------
  275. // Outputs texture value withtout lighting calculation.  Used for fullscreen
  276. // quad copy.
  277. float4 PixNoLight( float2 Tex : TEXCOORD0 ) : COLOR
  278. {
  279.     return tex2D( g_samScene, Tex );
  280. }
  281.  
  282.  
  283. //-----------------------------------------------------------------------------
  284. // Renders skybox.  Used only for geometry with a 3D texcoord.
  285. void RenderSkyBoxVS( float4 Pos : POSITION,
  286.                      float3 Tex : TEXCOORD0,
  287.                      out float4 oPos : POSITION,
  288.                      out float3 oTex : TEXCOORD0,
  289.                      out float3 oViewPos : TEXCOORD1 )
  290. {
  291.     oViewPos = mul( Pos, g_mWorldView );
  292.     oPos = mul( float4( oViewPos, 1.0f ), g_mProj );
  293.  
  294.     oTex = Tex;
  295. }
  296.  
  297.  
  298. void RenderSkyBoxPS( float3 Tex : TEXCOORD0,
  299.                      float3 ViewPos : TEXCOORD1,
  300.                      out float4 oColor : COLOR0,
  301.                      out float4 oNormal : COLOR1,
  302.                      out float4 oPos : COLOR2 )
  303. {
  304.     //
  305.     // Output pixel color
  306.     //
  307.     oColor = texCUBE( g_samSkyBox, Tex );
  308.  
  309.     //
  310.     // Output normal has (0, 0, -1) always
  311.     //
  312.     oNormal = float4( 0.0f, 0.0f, -1.0f, 1.0f );
  313.  
  314.     //
  315.     // Output view position
  316.     //
  317.     oPos = float4( ViewPos, 1.0f );
  318. }
  319.  
  320.  
  321. void RenderSkyBoxPS_ColorNormal( float3 Tex : TEXCOORD0,
  322.                                  float3 ViewPos : TEXCOORD1,
  323.                                  out float4 oColor : COLOR0,
  324.                                  out float4 oNormal : COLOR1 )
  325. {
  326.     //
  327.     // Output pixel color
  328.     //
  329.     oColor = texCUBE( g_samSkyBox, Tex );
  330.  
  331.     //
  332.     // Output normal has (0, 0, -1) always
  333.     //
  334.     oNormal = float4( 0.0f, 0.0f, -1.0f, 1.0f );
  335. }
  336.  
  337.  
  338. void RenderSkyBoxPS_Color( float3 Tex : TEXCOORD0,
  339.                            float3 ViewPos : TEXCOORD1,
  340.                            out float4 oColor : COLOR0 )
  341. {
  342.     //
  343.     // Output pixel color
  344.     //
  345.     oColor = texCUBE( g_samSkyBox, Tex );
  346. }
  347.  
  348.  
  349. void RenderSkyBoxPS_Normal( float3 Tex : TEXCOORD0,
  350.                             float3 ViewPos : TEXCOORD1,
  351.                             out float4 oNormal : COLOR0 )
  352. {
  353.     //
  354.     // Output normal has (0, 0, -1) always
  355.     //
  356.     oNormal = float4( 0.0f, 0.0f, -1.0f, 1.0f );
  357. }
  358.  
  359.  
  360. void RenderSkyBoxPS_Position( float3 Tex : TEXCOORD0,
  361.                               float3 ViewPos : TEXCOORD1,
  362.                               out float4 oPos : COLOR0 )
  363. {
  364.     //
  365.     // Output view position
  366.     //
  367.     oPos = float4( ViewPos, 1.0f );
  368. }
  369.  
  370.  
  371. //-----------------------------------------------------------------------------
  372. // Technique: RenderScene
  373. // Desc: Renders the scene with a vertex and pixel shader.
  374. //-----------------------------------------------------------------------------
  375. technique RenderScene
  376. {
  377.     pass p0
  378.     {
  379.         VertexShader = compile vs_1_1 RenderSceneVS();
  380.         PixelShader = compile ps_2_0 RenderScenePS();
  381.         ZEnable = true;
  382.     }
  383. }
  384.  
  385.  
  386. technique RenderSceneTwoPasses
  387. {
  388.     pass p0
  389.     {
  390.         VertexShader = compile vs_1_1 RenderSceneVS();
  391.         PixelShader = compile ps_2_0 RenderScenePS_ColorNormal();
  392.         ZEnable = true;
  393.     }
  394.     pass p1
  395.     {
  396.         VertexShader = compile vs_1_1 RenderSceneVS();
  397.         PixelShader = compile ps_2_0 RenderScenePS_Position();
  398.     }
  399. }
  400.  
  401.  
  402. technique RenderSceneThreePasses
  403. {
  404.     pass p0
  405.     {
  406.         VertexShader = compile vs_1_1 RenderSceneVS();
  407.         PixelShader = compile ps_2_0 RenderScenePS_Color();
  408.         ZEnable = true;
  409.     }
  410.     pass p1
  411.     {
  412.         VertexShader = compile vs_1_1 RenderSceneVS();
  413.         PixelShader = compile ps_2_0 RenderScenePS_Normal();
  414.     }
  415.     pass p2
  416.     {
  417.         VertexShader = compile vs_1_1 RenderSceneVS();
  418.         PixelShader = compile ps_2_0 RenderScenePS_Position();
  419.     }
  420. }
  421.  
  422.  
  423. //-----------------------------------------------------------------------------
  424. // Technique: RenderEnvMapScene
  425. // Desc: Renders the scene with with environment mapping.
  426. //-----------------------------------------------------------------------------
  427. technique RenderEnvMapScene
  428. {
  429.     pass p0
  430.     {
  431.         VertexShader = compile vs_1_1 RenderEnvMapSceneVS();
  432.         PixelShader = compile ps_2_0 RenderEnvMapScenePS();
  433.         ZEnable = true;
  434.     }
  435. }
  436.  
  437.  
  438. technique RenderEnvMapSceneTwoPasses
  439. {
  440.     pass p0
  441.     {
  442.         VertexShader = compile vs_1_1 RenderEnvMapSceneVS();
  443.         PixelShader = compile ps_2_0 RenderEnvMapScenePS_ColorNormal();
  444.         ZEnable = true;
  445.     }
  446.     pass p1
  447.     {
  448.         VertexShader = compile vs_1_1 RenderEnvMapSceneVS();
  449.         PixelShader = compile ps_2_0 RenderEnvMapScenePS_Position();
  450.     }
  451. }
  452.  
  453.  
  454. technique RenderEnvMapSceneThreePasses
  455. {
  456.     pass p0
  457.     {
  458.         VertexShader = compile vs_1_1 RenderEnvMapSceneVS();
  459.         PixelShader = compile ps_2_0 RenderEnvMapScenePS_Color();
  460.         ZEnable = true;
  461.     }
  462.     pass p1
  463.     {
  464.         VertexShader = compile vs_1_1 RenderEnvMapSceneVS();
  465.         PixelShader = compile ps_2_0 RenderEnvMapScenePS_Normal();
  466.     }
  467.     pass p2
  468.     {
  469.         VertexShader = compile vs_1_1 RenderEnvMapSceneVS();
  470.         PixelShader = compile ps_2_0 RenderEnvMapScenePS_Position();
  471.     }
  472. }
  473.  
  474.  
  475. technique RenderNoLight
  476. {
  477.     pass p0
  478.     {
  479.         VertexShader = null;
  480.         PixelShader = compile ps_1_1 PixNoLight();
  481.         ZEnable = false;
  482.     }
  483. }
  484.  
  485.  
  486. //-----------------------------------------------------------------------------
  487. // Technique: RenderSkyBox
  488. // Desc: Renders using 3D texture coordinates to sample from a cube texture.
  489. //-----------------------------------------------------------------------------
  490. technique RenderSkyBox
  491. {
  492.     pass p0
  493.     {
  494.         VertexShader = compile vs_1_1 RenderSkyBoxVS();
  495.         PixelShader = compile ps_2_0 RenderSkyBoxPS();
  496.         ZEnable = true;
  497.     }
  498. }
  499.  
  500.  
  501. technique RenderSkyBoxTwoPasses
  502. {
  503.     pass p0
  504.     {
  505.         VertexShader = compile vs_1_1 RenderSkyBoxVS();
  506.         PixelShader = compile ps_2_0 RenderSkyBoxPS_ColorNormal();
  507.         ZEnable = true;
  508.     }
  509.     pass p1
  510.     {
  511.         VertexShader = compile vs_1_1 RenderSkyBoxVS();
  512.         PixelShader = compile ps_2_0 RenderSkyBoxPS_Position();
  513.     }
  514. }
  515.  
  516.  
  517. technique RenderSkyBoxThreePasses
  518. {
  519.     pass p0
  520.     {
  521.         VertexShader = compile vs_1_1 RenderSkyBoxVS();
  522.         PixelShader = compile ps_2_0 RenderSkyBoxPS_Color();
  523.         ZEnable = true;
  524.     }
  525.     pass p1
  526.     {
  527.         VertexShader = compile vs_1_1 RenderSkyBoxVS();
  528.         PixelShader = compile ps_2_0 RenderSkyBoxPS_Normal();
  529.     }
  530.     pass p2
  531.     {
  532.         VertexShader = compile vs_1_1 RenderSkyBoxVS();
  533.         PixelShader = compile ps_2_0 RenderSkyBoxPS_Position();
  534.     }
  535. }
  536.